package cn.itcast.controller;

/*
 *Author:TH
 *Dete:2019/10/2314:59
 */

import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@SuppressWarnings("all")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    //上传图片到七牛云服务器
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);
        String fileName = UUID.randomUUID().toString() + extention;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //上传成功.保存图片名称到redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            //返回成功消息以及图片姓名
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加检查项
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")//权限校验
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        System.out.println("*************************");
        //用try catch来判断是否执行成功
        try{
            setmealService.add(setmeal,checkgroupIds);
            //保存成功,存入redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

        }catch (Exception e){
            //添加失败
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        //添加成功,返回消息
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findByPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        Integer cp = queryPageBean.getCurrentPage();
        Integer ps = queryPageBean.getPageSize();
        String qs = queryPageBean.getQueryString();
        PageResult pageResult = setmealService.pageQuery(cp,ps,qs);

        return pageResult;
    }


    //按照id查询检查组
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public Result findBysId(Integer id){
        try {
            Setmeal setmeal = setmealService.findBysId(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //按照id查询检查项
    @RequestMapping("/findCheckGroupIdsBysetmealId")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public Result findCheckGroupIdsBysetmealId(Integer id){
        try {
            List<Integer> checkgroupIds= setmealService.findCheckGroupIdsBysetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //查找全部
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public Result findAll(){

        List<Setmeal> setmealList =setmealService.findAll();
        if (setmealList!=null&&setmealList.size()>0){
            //查询成功
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,setmealList);
        }else{
            //查询失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //按照id查询检查项
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")//权限校验
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

}
